package com.knight.controller;

import com.jfinal.core.Controller;
import com.knight.common.constant.Constant;
import com.knight.model.Message;
import com.knight.response.JsonMessage;
import com.knight.service.MessageService;

/**
 * 留言Controller
 * 
 * @author YQ
 *
 */
public class CustomerMessageController extends Controller {

	private MessageService service = MessageService.getInstance();

	public void save() {

		JsonMessage jsonMessage = new JsonMessage();

		Message message = getModel(Message.class);

		if (message == null) {
			jsonMessage.setSuccess(Constant.JsonConstant.SUCCESS_ERROR);
			jsonMessage.setErrorMessage("请求参数错误");
			renderJson(jsonMessage);
			return;
		}

		String clientName = message.get("clientName");

		if (clientName == null || "".equals(clientName)) {
			jsonMessage.setSuccess(Constant.JsonConstant.SUCCESS_ERROR);
			jsonMessage.setErrorMessage("请填写客户姓名");
			renderJson(jsonMessage);
			return;
		}

		String phone = message.get("phone");
		
		if (phone == null || "".equals(phone)) {
			jsonMessage.setSuccess(Constant.JsonConstant.SUCCESS_ERROR);
			jsonMessage.setErrorMessage("请输入手机号或电话,方便联系");
			renderJson(jsonMessage);
			return;
		}
		
/*		String company = message.get("company");
		
		if (company == null || "".equals(company)) {
			jsonMessage.setSuccess(Constant.JsonConstant.SUCCESS_ERROR);
			jsonMessage.setErrorMessage("请填写公司名称");
			renderJson(jsonMessage);
			return;
		}*/
		
		String content = message.get("content");
		
		if (content == null || "".equals(content)) {
			jsonMessage.setSuccess(Constant.JsonConstant.SUCCESS_ERROR);
			jsonMessage.setErrorMessage("请填写咨询内容");
			renderJson(jsonMessage);
			return;
		}
		
		boolean flag = service.save(message);
		if (flag) {
			jsonMessage.setSuccess(Constant.JsonConstant.SUCCESS_SUCCESS);
			renderJson(jsonMessage);
		} else {
			jsonMessage.setSuccess(Constant.JsonConstant.SUCCESS_ERROR);
			renderJson(jsonMessage);
		}
	}

}
